package yxy.game.pm2.active.command.ghost;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actghosttasknumreward;
import yxy.cherry.data.bean.actghosttaskreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_神魔卡;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.ghost.GhostActive;
import yxy.game.pm2.active.module.subordinateclass.ghost.GhostTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**
 * 神魔卡_抽将任务
 * @author Dell
 *
 */
@MessageType(proto = Command.ActGhostActiveTaskInfo, classtype = ActiveMessage.ActGhostActiveTaskInfoRequest.class)
public class CMD_Active_神魔卡_活跃任务_Info extends Command<ActiveMessage.ActGhostActiveTaskInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_神魔卡_活跃任务_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActGhostActiveTaskInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActGhostActiveTaskInfoResponse.Builder response = ActiveMessage.ActGhostActiveTaskInfoResponse.newBuilder();
		
		ActiveModule_神魔卡 am = ActiveModule_神魔卡.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		GhostActive ga = am.getGa();
		
		//活跃任务
		for(GhostTask gt : ga.getActivemap().values()) {
			ActiveMessage.ActiveTask.Builder build = ActiveMessage.ActiveTask.newBuilder();
			build.setId(gt.getId());
			build.setTaskid(gt.getTaskid());
			build.setPosition(gt.getPosition());
			
			String taskcode = gt.getTaskcode();
			Task taskbean = Task.get(playercode, taskcode);
			build.setProgress(taskbean.getProgress());
			int canget = taskbean.isReward() ? DrawStatus.hadget.getStatus() : taskbean.isFinish() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);

			response.addActivetask(build.build());
		}
		
		//活跃任务组合宝箱
		Map<String, actghosttaskreward> agtrmap = ActDataCenter.getAllData(actghosttaskreward.class, playercode);
		for(actghosttaskreward data : agtrmap.values()) {
			ActiveMessage.ZhBox.Builder build = ActiveMessage.ZhBox.newBuilder();
			build.setId(data.getId());
			
			Collection<Currency> curs = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			
			ArrayList<Integer> needpositions = new ArrayList<Integer>();
			for(int needposition : data.getPositionid()) {
				needpositions.add(needposition);
			}
			build.addAllPositions(needpositions);
			
			int canget = DrawStatus.cannotget.getStatus();
			if(ga.getZhgetids().contains(data.getId())) {
				canget = DrawStatus.hadget.getStatus();
			}else {
				if(needpositions.size() > 0 && ga.getFinishpositions().containsAll(needpositions)) {
					canget = DrawStatus.canget.getStatus();
				}else {
					canget = DrawStatus.cannotget.getStatus();
				}	
			}
			build.setCanget(canget);
			
			response.addZbs(build.build());
		}
		
		//活跃任务完成数宝箱
		response.setFinishactivetasknum(ga.getZhboxgetnum());
		Map<String, actghosttasknumreward> agtnrmap = ActDataCenter.getAllData(actghosttasknumreward.class, playercode);
		for(actghosttasknumreward data : agtnrmap.values()) {
			ActiveMessage.TaskNumBox.Builder build = ActiveMessage.TaskNumBox.newBuilder();
			build.setId(data.getId());
			build.setCondition(data.getCondition());

			Collection<Currency> curs = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			build.addAllCurs(items);
			
			int canget = ga.getFinishnumboxgetids().contains(data.getId()) ? DrawStatus.hadget.getStatus() : ga.getZhboxgetnum() >= data.getCondition() ?
					DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);
			
			response.addTnbs(build.build());
		}

		response.setStart(am.getTimeline().getStartTime().getMillis());
		response.setEnd(am.getTimeline().getEndTime().getMillis());
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActGhostActiveTaskInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
